<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title data-i18n="resources.title_dataFlowService"></title>
        <script type="text/javascript" src="../js/include-web.js"></script>
        <script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
        <style>
            .ol-popup {
                position: absolute;
                background-color: white;
                -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
                filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
                padding: 15px;
                border-radius: 10px;
                border: 1px solid #cccccc;
                bottom: 12px;
                left: -50px;
                min-width: 210px;
            }

            .ol-popup:after,
            .ol-popup:before {
                top: 100%;
                border: solid transparent;
                content: " ";
                height: 0;
                width: 0;
                position: absolute;
                pointer-events: none;
            }

            .ol-popup:after {
                border-top-color: white;
                border-width: 10px;
                left: 48px;
                margin-left: -10px;
            }

            .ol-popup:before {
                border-top-color: #cccccc;
                border-width: 11px;
                left: 48px;
                margin-left: -11px;
            }
        </style>
    </head>
    <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
        <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
        <div id="popup" class="ol-popup">
            <div id="popup-content"></div>
        </div>
        <script type="text/javascript">
            var container = document.getElementById("popup");
            var content = document.getElementById("popup-content");
            var overlay = new ol.Overlay({
                element: container,
                autoPan: true,
                autoPanAnimation: {
                    duration: 250
                }
            });
            var resultLayer,
                urlQuery = (window.isLocal ? window.server : "https://iserver.supermap.io") + "/iserver/services/map-china/rest/maps/China_4326";
            var urlDataFlow = "wss://iclsvrws.supermap.io/iserver/services/dataflowTest/dataflow";
            var timer, featureResult, dataFlowBroadcast, source;
            new ol.supermap.MapService(urlQuery).getMapInfo(function(serviceResult) {
                var mapJSONObj = serviceResult.result;
                //初始化地图
                var map = window.map = new ol.Map({
                    target: "map",
                    // ol v7版本用法为ol.control.defaults.defaults； v6版本以下用法为ol.control.defaults
                    controls: ol.control.defaults.defaults({
                            attributionOptions: {
                                collapsed: false
                            }
                        })
                        .extend([new ol.supermap.control.Logo({ link: "https://iclient.supermap.io" })]),
                    view: new ol.View({
                        center: [117.26359642356829, 39.323798688605166],
                        zoom: 10,
                        projection: "EPSG:4326",
                        multiWorld: true
                    }),
                    overlays: [overlay]
                });
                //添加底图
                var layer = new ol.layer.Tile({
                    source: new ol.source.TileSuperMapRest(ol.source.TileSuperMapRest.optionsFromMapJSON(urlQuery, mapJSONObj))
                });
                map.addLayer(layer);

                ol.supermap.SecurityManager.registerToken(urlDataFlow, window.exampleToken);

                //添加dataflow
                var source = new ol.source.DataFlow({
                    ws: urlDataFlow
                });
                source.on("dataupdated", function(e) {
                    var feature = e.value.data;
                    content.innerHTML = feature.get("time");
                    overlay.setPosition(feature.getGeometry().getCoordinates());
                });
                resultLayer = new ol.layer.Vector({
                    source: source,
                    style: new ol.style.Style({
                        image: new ol.style.Circle({
                            fill: new ol.style.Fill({
                                color: "rgba(255,0,0,0.9)"
                            }),
                            radius: 6
                        })
                    })
                });

                //模拟实时数据 start
                //查询一个线数据，每两秒将一个点通过dataFlowService广播给iSevrer的dataflow服务
                query();
                function query() {
                    var param = new ol.supermap.QueryBySQLParameters({
                        queryParams: {
                            name: "ProvincialRd_L@China#1",
                            attributeFilter: "SMID = 2504"
                        }
                    });
                    new ol.supermap.QueryService(urlQuery).queryBySQL(param).then(function(serviceResult) {
                        featureResult = serviceResult;
                        dataFlowBroadcast = new ol.supermap.DataFlowService(urlDataFlow).initBroadcast();
                        dataFlowBroadcast.on("broadcastSocketConnected", function(e) {
                            timer = window.setInterval("broadcast()", 2000);
                        });
                        map.addLayer(resultLayer);
                    });
                }
            });
            var count = 200;
            function broadcast() {
                if (count >= featureResult.result.recordsets[0].features.features[0].geometry.coordinates.length) {
                    window.clearInterval(timer);
                    return;
                }
                var point = featureResult.result.recordsets[0].features.features[0].geometry.coordinates[count];
                var feature = {
                    geometry: {
                        coordinates: [point[0], point[1]],
                        type: "Point"
                    },
                    type: "Feature",
                    properties: {
                        id: 1,
                        time: new Date()
                    }
                };
                dataFlowBroadcast.broadcast(feature);
                count += 3;
            }
            //模拟实时数据 end
        </script>
    </body>
</html>
